package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ConsumerMapper;
import com.guigu.mapper.MerchantMapper;
import com.guigu.pojo.Consumer;
import com.guigu.pojo.Merchant;
import com.guigu.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    @Override
    public IPage<Merchant> getMerchantPage(Page<Merchant> page, String shopName, String shopType, String startDate, String endDate) {
        return baseMapper.selectMerchantPage(page, shopName, shopType, startDate, endDate);
    }

    @Override
    public boolean updateMerchant(Merchant merchant) {
        return updateById(merchant);
    }

    @Override
    public boolean deleteMerchant(Integer merchantId) {
        return removeById(merchantId);
    }

    @Override
    public IPage<Merchant> getMerchantPage2(Page<Merchant> page, String shopName, String shopType, String startDate, String endDate, String checkTag) {
        return baseMapper.MerchantPage(page, shopName, shopType, startDate, endDate, checkTag);
    }

    @Override
    public boolean review(Merchant merchant) {
        if (merchant.getCheckTag().equals("S001-1") || merchant.getCheckTag().equals("S001-2")) {
            merchant.setCheckTag("S001-3");
            merchant.setChecker("admin");
            merchant.setCheckerTime(new Date());

            Integer consumerId = merchant.getConsumerId();
            Consumer consumer = consumerMapper.selectById(consumerId);
            consumer.setMerchantTag("S001-3");
            consumerMapper.updateById(consumer);

            return updateById(merchant);
        }
        return false;
    }

    @Override
    public List<Merchant> getMerchantWithNickname(String shopName, String shopType, String startDate, String endDate) {
        return baseMapper.selectMerchantWithNickname(shopName, shopType, startDate, endDate);
    }

    @Override
    public Merchant selectMerchantWithNicknameById(Integer merchantId) {
        return baseMapper.selectMerchantWithNicknameById(merchantId);
    }
}
